var pageSize = 10;
//var BASEURL = 'http://zhixian.f3322.org:81/module-interface';
//var BASEURL = 'http://api.feng.com:8080/module-interface';
var BASEURL = '';
//var BASEURL = 'http://api.zhixianren.com:8080/module-interface';
var PLATFORM = 'platform=e885a95b9a074d048dcedb5b7aa38b2b';
var TRAIN_NORMAL = 'images/train_normal.jpg';
var TRAIN_NORMAL_MAIN = 'images/train_normal_main.jpg';
var TRAIN_NORMAL_LIST = 'images/train_normal_list.jpg';
var COURSE_NORMAL = 'images/course_normal.jpg';
var COURSE_NORMAL_MAIN = 'images/course_normal_main.jpg';
var COURSE_NORMAL_LIST = 'images/course_normal_list.jpg';
var HEAD_NORMAL = 'images/default_avatar.png';
var NEW_SMALL_NORMAL = 'images/default_new.jpg';
var NEW_BIG_NORMAL = 'images/default_new_big.jpg';


/**
 * 毫秒转时间格式
 */
function longMsTimeConvertToDateTime(time) {
    if(time) {
        var myDate = new Date(time);
        return this.formatterDateTime(myDate);
    }
    return "-";
}
/**
 * 格式化去日期（含时间）
 */
function formatterDateTime(date) {
    var datetime = date.getFullYear()
        + "-"// "年"
        + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
        + (date.getMonth() + 1))
        + "-"// "月"
        + (date.getDate() < 10 ? "0" + date.getDate() : date
            .getDate())
        + " "
        + (date.getHours() < 10 ? "0" + date.getHours() : date
            .getHours())
        + ":"
        + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date
            .getMinutes())
        + ":"
        + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date
            .getSeconds());
    return datetime;
}

function formatDateToMs(date) {
    var now = new Date(date);
    // var year = now.getYear();
    // var month = now.getMonth() + 1;
    // var date = now.getDate();
    var hour = "00" + now.getHours();
    var minute = "00" + now.getMinutes();
    var second = "00" + now.getSeconds();
    var ms = "000" + now.getMilliseconds();
    return hour.slice(-2)+":"+minute.slice(-2) + ":" + second.slice(-2) + " " + ms.slice(-3);
}


var ajax = {
    ajax: function (type, uri, p, callback) {
        var windex = layer.load(1, {shade: [0.05, '#000']});
        //var windex = layer.msg('加载中', {icon: 16,shade: 0.05});
        $.ajax({
            url: uri,
            data: p,
            type: type,
            success: function (data) {
                layer.close(windex);
                if (data.code == 0) {
                    if (callback) {
                        callback(data);
                    }
                } else if (data.code == -588) { // 超时
                    layer.msg("登录超时,请刷新浏览器...");
                } else {
                    layer.msg(data.error);
                }
            },
            error: function () {
                layer.msg('网络连接失败');
            }
        });
    },

    post: function (uri, p, callback) {
        this.ajax("post", uri, p, callback);
    },

    get: function (uri, p, callback) {
        this.ajax("get", uri, p, callback);
    }
}

function setPage(data) {
    var pageUl = $("#pageUl");
    pageUl.html('');
    if(data.pages == 0) {
        return;
    }
    if(data.pageNum==1) {
        $('<li class="disabled"><a href="javascript:void(0)">&laquo;</a></li>').appendTo(pageUl);
        $('<li class="disabled"><a href="javascript:void(0)">&lsaquo;</a></li>').appendTo(pageUl);
    } else {
        $('<li><a href="javascript:toPage(1)">&laquo;</a></li>').appendTo(pageUl);
        $('<li><a href="javascript:toPage('+(data.pageNum-1)+')">&lsaquo;</a></li>').appendTo(pageUl);
    }
    var i = data.pageNum-2<=1?1:data.pageNum-2;
    for(var i; i <= data.pages && i <= data.pageNum+3; i ++) {
        if(data.pageNum == i) {
            $('<li class="active"><a href="javascript:toPage('+i+')">'+i+'</a></li>').appendTo(pageUl);
        } else {
            $('<li><a href="javascript:toPage('+i+')">'+i+'</a></li>').appendTo(pageUl);
        }
    }
    if(data.pageNum==data.pages) {
        $('<li class="disabled"><a href="javascript:void(0)">&rsaquo;</a></li>').appendTo(pageUl);
        $('<li class="disabled"><a href="javascript:void(0)">&raquo;</a></li>').appendTo(pageUl);
    } else {
        $('<li><a href="javascript:toPage('+(data.pageNum+1)+')">&rsaquo;</a></li>').appendTo(pageUl);
        $('<li><a href="javascript:toPage('+(data.pages)+')">&raquo;</a></li>').appendTo(pageUl);
    }
    $('<span style="line-height:35px;margin-left:10px;">共计' + data.total + '条</span>').appendTo(pageUl);
}